import requests

app_id = "wx33258b0c90d5d622"
app_secret = "a3a43c187fc399694580b6edc129ae16"
template_id = "5MixIFAm20bFw06ZUJaO9ad551CENNA8u3UIq0JWQJs"

prefix = "https://api.weixin.qq.com/cgi-bin/"
# 获取token
def get_access_token():
    url = prefix + "token?grant_type=client_credential&appid={}&secret={}".format(app_id, app_secret)
    resp = requests.get(url).json()
    return resp["access_token"]
# 获取关注用户
def get_open_ids(access_token):
    url = prefix + "user/get?access_token={}".format(access_token)
    resp = requests.get(url)
    return resp.json()['data']['openid']
# 给指定关注用户发送
def send_msg(access_token, open_id, data_dict):
    url = prefix + "message/template/send?access_token={}".format(access_token)
    data = {
        "touser": open_id,
        "template_id": template_id,
        "data": {}
    }
    for key in data_dict:
        data["data"][key] = {"value": data_dict[key]}
    requests.post(url, json=data)

token = get_access_token()
access_ids = get_open_ids(token)
send_msg(token, access_ids[0], {"body": "还不关注？！"})
